package com.example.cstr.service.serviceImpl;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Numberings;
import com.example.cstr.config.FileConfig;
import com.example.cstr.entity.*;
import com.example.cstr.mapper.EducationTrainClassMapper;
import com.example.cstr.mapper.EducationTrainProjectMapper;
import com.example.cstr.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import com.example.cstr.utils.FileUtils;

import com.example.cstr.mapper.StudyContainMapper;
import com.example.cstr.service.StudyContainService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudyContainServiceImpl implements StudyContainService {

    @Resource
    private StudyContainMapper studyContainMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private EducationTrainClassMapper educationTrainClassMapper;

    @Resource
    private EducationTrainProjectMapper educationTrainProjectMapper;

    @Resource
    private FileConfig config;

    @Override
    public int deleteByPrimaryKey(Integer scId) {
        return studyContainMapper.deleteByPrimaryKey(scId);
    }

    @Override
    public int insert(StudyContain record) {
        return studyContainMapper.insert(record);
    }

    @Override
    public int insertSelective(StudyContain record) {
        studyContainMapper.insertSelective(record);
        return record.getScId();
    }

    @Override
    public StudyContain selectByPrimaryKey(Integer scId) {
        return studyContainMapper.selectByPrimaryKey(scId);
    }

    @Override
    public int updateByPrimaryKeySelective(StudyContain record) {
        return studyContainMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(StudyContain record) {
        return studyContainMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<StudyContain> selectAllStudyContain() {
        return studyContainMapper.selectAllStudyContain();
    }

    @Override
    public Message PagingSearch(@RequestParam Integer pageNum, @RequestParam Integer size) {
        PageHelper.startPage(pageNum, size);
        List<StudyContain> studyContainList = studyContainMapper.selectAllStudyContain();
        PageInfo pageInfo = new PageInfo(studyContainList);
        pageInfo.getTotal();
        return Message.success(null).add(pageInfo);
    }

    @Override
    public List<StudyContain> selectBySc_work_idAndSc_etp_id(Integer sc_work_id, Integer sc_etp_id) {
        return studyContainMapper.selectBySc_work_idAndSc_etp_id(sc_work_id, sc_etp_id);
    }

    @Override
    public Message pagingSearch_BySc_work_idAndSc_etp_id(@RequestParam Integer pageNum, @RequestParam Integer size, Integer sc_work_id, Integer sc_etp_id) {
        PageHelper.startPage(pageNum, size);
        List<StudyContain> studyContainList = studyContainMapper.selectBySc_work_idAndSc_etp_id(sc_work_id, sc_etp_id);
        PageInfo pageInfo = new PageInfo(studyContainList);
        pageInfo.getTotal();
        return Message.success(null).add(pageInfo);
    }

    @Override
    public Message getWord(Integer workId) throws IOException {
        //获取用户及表中数据
        List<StudyContain> studyContainList = studyContainMapper.selectAllByScWorkId(workId);
        User user = userMapper.selectByPrimaryKey(workId);
        List<EducationTrainClass> educationTrainClassList = educationTrainClassMapper.selectEtcIdByWorkId(workId);
        List<EducationTrainProject> educationTrainProjectList = educationTrainProjectMapper.selectEtpIdIdByWorkId(workId);
        //获取模板
        ClassPathResource classPathResource = new ClassPathResource("北京城市学院干部教育培训学时情况登记表.docx");
        //格式化日期
//        DateFormat df = new SimpleDateFormat("yyyy.MM");
        //定义总学时
        Integer total = 0;
        //拼接学习内容概要和完成学时数
        StringBuffer sb = new StringBuffer();
        for (StudyContain contain : studyContainList) {
            sb.append(contain.getScFormString() + "," + contain.getBeforeExchangeValue() + "学时;");
            total += contain.getAfterExchangeStudyTime();
        }
        Integer finalTotal = total;
        String[] strings = sb.toString().split(";");
        //拼接学习项目信息
        StringBuffer str = new StringBuffer();
        for (EducationTrainProject eTrainProject : educationTrainProjectList) {
            str.append(eTrainProject.getEtpName() + "(" + eTrainProject.getEtpExchangeRule() + ")");
            str.append(";");
        }
        String[] strings1 = str.toString().split(";");
        //渲染数据
        Map<String, Object> data = new HashMap<String, Object>() {
            {
                put("year", "2020-2021");
                put("name", user.getUserName());
                put("sex", user.getGender());
                put("age", user.getUserAge());
                put("dep", user.getUserDepartment());
                put("job", user.getUserJob());
                for (EducationTrainClass educationTrainClass : educationTrainClassList) {
                    put("etc", educationTrainClass.getEtcName());
                }
                put("str", Numberings.ofDecimal(strings1).create());
                put("list", Numberings.ofDecimal(strings).create());
                put("time", finalTotal + "学时");
            }
        };
        //导出word
        XWPFTemplate template = XWPFTemplate.compile(classPathResource.getInputStream()).render(data);
        String outputName = user.getUserName() + "——北京城市学院干部教育培训学时情况登记表.docx";
//        String outputName="1.docx";
        FileOutputStream out;
        System.out.println(config.FilePath + outputName);
        try {
            out = new FileOutputStream(config.FilePath + outputName);
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

//        return Message.success(null).add(config.hostUrl + "/resource/" + outputName);
        return Message.success(null).add(config.hostUrl + "/upload_file/" + outputName);
    }

    @Override
    public List<StudyContain> selectBySc_etc_idAndSc_etp_id(Integer sc_etc_id, Integer sc_etp_id) {
        return studyContainMapper.selectBySc_etc_idAndSc_etp_id(sc_etc_id, sc_etp_id);
    }


}














